#include"TeamGroup.h"
#include"../jsonCpp/json.h"
#include"../event/SignalManager.h"
#include"../Common.h"

int TeamGroup::MAX_MEMBERS = 4;

TeamGroup::TeamGroup()
{
	m_teamId = 0;
	m_leaderId = 0;
	m_teamTargetId = 0;
}

TeamGroup::TeamGroup(int teamId, int leaderId, int targetId)
{
	m_teamId = teamId;
	m_leaderId = leaderId;
	m_teamTargetId = targetId;
	addMember(m_leaderId);
}

TeamGroup::~TeamGroup()
{
}

void TeamGroup::setController(Object* controller)
{
	m_controller = controller;
}

bool TeamGroup::addMember(int userId)
{
	if (m_members.size() == MAX_MEMBERS)
	{
		return false;
	}
	m_members.push_back(userId);
}

bool TeamGroup::removeMember(int userId)
{
	for (auto iter = m_members.begin(); iter != m_members.end(); ++iter)
	{
		if (*iter == userId)
		{
			auto monsterList = getMonsterList();
			for (auto iter = monsterList->begin(); iter != monsterList->end(); ++iter)
			{
				iter->removeCanSee(userId);
				if (iter->getTrackId() == userId)
				{
					iter->setTrackId(0);
					iter->setFight(false);
					Json::Value message;
					message["sceneId"] = m_monsterGroup.getSceneId();
					message["monsterId"] = iter->getId();
					message["teamId"] = m_teamId;
					SignalManager::getInstance()->dispatch(MONSTER_REMOVE_FIGHT_NOTIFY, message);
				}
			}
			m_members.erase(iter);
			return true;
		}
	}
	return false;
}

const std::vector<int>& TeamGroup::getMembers()
{
	return m_members;
}

int TeamGroup::getLeaderId()
{
	return m_leaderId;
}

int TeamGroup::getTargetId()
{
	return m_teamTargetId;
}

void TeamGroup::setMonsterGroupId(const int& id, const int& sceneId)
{
	m_monsterGroup.init(id, sceneId);
}

int TeamGroup::getMonsterGroupId()
{
	return m_monsterGroup.getId();
}

std::list<GameMonster>* TeamGroup::getMonsterList()
{
	return m_monsterGroup.getMonsterList();
}
